/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package as24gc.zip;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

/**
 *
 * @author nico.vassallo
 */
public class ZipFolder {

    public static String zipFolder(String inFolderName) {
        String result = null;
        String zipFileName = inFolderName+".zip";
        try {
            System.out.println("inFolerName: "+inFolderName);
            File inFolder = new File(inFolderName);
           
            
            File outFolder = new File(zipFileName);
            ZipOutputStream out = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(outFolder)));
            BufferedInputStream in = null;
            byte[] data = new byte[1000];
            String files[] = inFolder.list();
            for (int i = 0; i < files.length; i++) {
                System.out.println("files[i]: "+files[i]);
                in = new BufferedInputStream(new FileInputStream(inFolder.getPath() + "/" + files[i]), 1000);
                out.putNextEntry(new ZipEntry(files[i]));
                int count;
                while ((count = in.read(data, 0, 1000)) != -1) {
                    out.write(data, 0, count);
                }
                out.closeEntry();
            }
            out.flush();
            out.close();
            System.out.println("Zip with SUCCESS");
            result = zipFileName;
        } catch (Exception e) {
            System.out.println("Zip ERROR"+e.getMessage());
            e.printStackTrace();
        }
        return result;
    }
}
